package com.dtagri.fjwlw.adapter;

import java.util.List;

import org.apache.http.Header;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;

import com.dtagri.fjwlw.adapter.viewholders.DeviceControlViewHolder;
import com.dtagri.fjwlw.api.FJwlwApi;
import com.dtagri.fjwlw.base.TGson;
import com.dtagri.fjwlw.bean.ErrorMsg;
import com.dtagri.fjwlw.bean.SmallArea;
import com.dtagri.fjwlw.bean.TjEquipment;
import com.dtagri.fjwlw.fragment.DeviceControlFragment;
import com.dtagri.fjwlw.util.TToast;
import com.dtagri.fjwlw.widget.togglebutton.ToggleButton.OnToggleChanged;
import com.loopj.android.http.AsyncHttpResponseHandler;

/**
 * 设备控制适配器
 * @author wxjn
 * 创建日期：
 * 修改日： 2015-09-17
 * 修改人:CL.
 */
public class DeviceControlAdapter extends
		CommonAdapter<SmallArea, DeviceControlViewHolder> {

	private DeviceControlFragment parentFragment;// 可读性差，但这样方便
	private Context mContext;

	private final AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {
			TToast.showToastShort("操作失败");
		}

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			ErrorMsg fromJson = TGson.fromJson(ErrorMsg.class, arg2);
			TToast.showToastShort(fromJson.getMsg());
		}

	};

	public DeviceControlAdapter(Context context, DeviceControlFragment parent,
			List<SmallArea> mDataSet) {
		super(context, mDataSet);
		mContext = context;
		this.parentFragment = parent;
	}

	@Override
	public int getCount() {
		int i = 0;
		for (SmallArea smallArea : mDataSet) {
			List<TjEquipment> tjEquipments = smallArea.getTjEquipments();
			i += tjEquipments.size();
		}
		return i;
	}

	@Override
	protected DeviceControlViewHolder createViewHolder() {
		return new DeviceControlViewHolder();
	}

	@Override
	protected void setItemData(int position,
			DeviceControlViewHolder viewHolder, View rootView) {

		// 遍历树得到当前大区\小区\传感器
		int i = 0;
		for (SmallArea smallArea : mDataSet) {
			List<TjEquipment> tjEquipments = smallArea.getTjEquipments();
			int j = 0;
			for (final TjEquipment tjEquipment : tjEquipments) {
				if (position == i) {
					viewHolder.tvArea.setText(smallArea.getSmallName());
					viewHolder.tvDeviceName.setText(tjEquipment.getEquipAlias() + "");
					
					if(tjEquipment.getNvrid()==null){
						viewHolder.controlVideo.setVisibility(View.INVISIBLE);
					}else{
						System.out.println("---NVRID-----"+i+":"+tjEquipment.getNvrid());
						
					}
					if (j == 0) {
						viewHolder.titleLine.setVisibility(View.VISIBLE);
					} else {
						viewHolder.titleLine.setVisibility(View.GONE);
					}
					if (tjEquipment.getSwitch_() == 0) {
						viewHolder.tbDevice.setToggleOff();
					} else {
						viewHolder.tbDevice.setToggleOn();
					}
					viewHolder.tbDevice.setOnToggleChanged(new OnToggleChanged() {

						@Override
						public void onToggle(boolean on) {
							//获取手机号，记录日志.
//							TelephonyManager tm = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE); 
//							String te1  = tm.getLine1Number();//获取本机号码  
//							String sm = tm.getSimSerialNumber();
//							System.err.println("telphone:"+te1+"  sm:"+sm);
							TToast.showToastShort("请稍等…");    
							//下发控制指令
							FJwlwApi.equipmentOperate(on ? 1 : 0,tjEquipment.getCeId(), mHandler);
						}
					});
					viewHolder.controlVideo.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							parentFragment.changeCamera(tjEquipment
									.getSschanelnum());
						}
					});
				}
				i++;
				j++;
			}
		}
	}

}
